package buber.genetic.op;

import java.util.Iterator;

import buber.genetic.interfaces.Chromosome;
import buber.genetic.interfaces.Member;
import buber.genetic.interfaces.Population;
import buber.genetic.op.interfaces.MutateOp;
import buber.util.MathUtils;

public class SimpleMutator implements MutateOp {
	public static final double GLOBAL_MUTATION_PROB=0.1;
	private double mutationProb=0;
	private boolean useMatrix=false;
	public SimpleMutator() {
		super();
		mutationProb=GLOBAL_MUTATION_PROB;
	}

	public double getMutationProb() {
		return mutationProb;
	}
	
	public void setMutationProb(double mutationProb) {
		this.mutationProb = mutationProb;
	}

	public Member mutate(Member m) {
		if(useMatrix){
			//
		}else{
			for(Iterator<Chromosome> cit=m.getChromosomes().iterator();cit.hasNext();){
				Chromosome ch = cit.next();
				for(int i=0;i<ch.getSize();i++){
					if(MathUtils.mutateWithProb(mutationProb)){
						ch.mutate(i);
					}
				}
			}
		}
		return m;
	}

	public Population doOperator(Population pop) {
		for(Iterator<Member> mit = pop.getMembers().iterator();mit.hasNext();){
			Member m = mit.next();
			for(Iterator<Chromosome> cit=m.getChromosomes().iterator();cit.hasNext();){
				Chromosome ch = cit.next();
				for(int i=0;i<ch.getSize();i++){
					if(MathUtils.mutateWithProb(mutationProb)){
						ch.mutate(i);
					}
				}
			}
		}
		return pop;
	}
	
}
